home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / Telnet 2.5.src.ThinkC / source / vsintern.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-05  |  35.1 KB  |  1,409 lines  |  [TEXT/MPS ]

  1. #ifdef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4.  
  5. /*
  6.  *
  7.  *      Virtual Screen Kernel Internal Routines
  8.  *                      (vsintern.c)
  9.  *  National Center for Supercomputing Applications
  10.  *
  11.  *      by Gaige B. Paulsen
  12.  *
  13.  *    This file contains the private internal calls for the NCSA
  14.  *  Virtual Screen Kernel.
  15.  *
  16.  *        Version Date    Notes
  17.  *        ------- ------  ---------------------------------------------------
  18.  *        0.01    861102  Initial coding -GBP
  19.  *        0.50    861113  First compiled edition -GBP
  20.  *        0.70    861114  Internal operation confirmed -GBP
  21.  *        2.1        871130    NCSA Telnet 2.1 -GBP
  22.  *        2.2     880715    NCSA Telnet 2.2 -GBP
  23.  */
  24.  
  25. #include <String.h>
  26. #include <Quickdraw.h>
  27. #include <Controls.h>
  28. #include <Memory.h>
  29. #include "vsdata.h"
  30. #include "vskeys.h"
  31. #include "vsinterf.h"
  32. #include "rsmac.h"
  33.  
  34. #define ScrollbackQuantum 100
  35.  
  36. #define VSIclrattrib 0
  37.  
  38. extern SysEnvRec theWorld;        /* BYU 2.4.12 - System Environment record */
  39. extern int isEightBit();        /* BYU 2.4.12 */
  40.  
  41. int VSIclip
  42.   (
  43.      int *x1, /* starting column */
  44.      int *y1, /* line on which to draw (assumed to lie within visible region) */
  45.      int *x2, /* ending column (inclusive) (output if *n >= 0) */
  46.      int *y2, /* ending line (inclusive) (output if *n >= 0) */
  47.      int *n, /* length of text to draw (input and output) */
  48.      int *offset /* length of initial part of text to skip (output) */
  49.   )
  50.   /* clips a text string to the visible region, given the starting
  51.     line and column in screen coordinates at which it is to be drawn.
  52.     If the length of the string is given, will also compute the ending
  53.     line and column. On return, these coordinates will be normalized
  54.     to the current visible region. Returns a nonzero function result
  55.     iff the string is completely invisible. */
  56.   {
  57.     if (*n >= 0)
  58.       {
  59.       /* compute ending line and column (inclusive) */
  60.         *x2 = *x1 + *n - 1;
  61.         *y2 = *y1;
  62.       }
  63.   /* else take these as given */
  64.  
  65.     if ((*x1 > VSIw->Rright) || (*y2 < VSIw->Rtop))
  66.         return (-1); /* nothing to draw */
  67.  
  68.     if (*x2 > VSIw->Rright)
  69.         *x2 = VSIw->Rright;
  70.     if (*y2 > VSIw->Rbottom)
  71.         *y2 = VSIw->Rbottom;
  72.   /* normalize x1, x2, y1, y2 to be relative to current visible region */
  73.     *x1 -= VSIw->Rleft;
  74.     *x2 -= VSIw->Rleft;
  75.     *y1 -= VSIw->Rtop;
  76.     *y2 -= VSIw->Rtop;
  77.   /* clip part of text string lying outside region, if any */
  78.     *offset = - *x1;
  79.     if (*offset < 0)
  80.         *offset = 0; /* text string starts within region--nothing to clip */
  81.   /* don't draw anything outside region */
  82.     if (*x1 < 0)
  83.         *x1 = 0;
  84.     if (*y1 < 0)
  85.         *y1 = 0;
  86.  
  87.     *n = *x2 - *x1  + 1 ; /* length of string to draw (assuming it's all on one line) */
  88.     if ((*n <= 0) || (*y2 - *y1 < 0))
  89.         return (-1); /* nothing to draw */
  90.     return (0);
  91.   } /* VSIclip */
  92.  
  93. VSIcdellines(w, top, bottom, n, scrolled)
  94.     int
  95.         w, /* affected window */
  96.         top, /* where to start deleting from */
  97.         bottom, /* bottom line of region to scroll */
  98.         n, /* number of lines to delete */
  99.         scrolled;
  100.           /*
  101.             -ve => cancel current selection, if any;
  102.             +ve => selection has moved up one line;
  103.             0 => don't touch selection
  104.           */
  105.   /* updates the display to indicate deletion of the specified
  106.     number of lines from the top of the specified region.
  107.     Returns 0 iff any part of the change is visible. */
  108.   {
  109.     int
  110.         x1 = 0,
  111.         x2 = VSIw->maxwidth,
  112.         tn = -1,
  113.         offset;
  114.  
  115.     if (VSIclip(&x1, &top, &x2, &bottom, &tn, &offset))
  116.         return(-1); /* affected region is invisible */
  117.     tn = bottom - top;
  118.     if (tn < n)
  119.         n = tn; /* don't bother scrolling more lines than scrolling region holds */
  120.     RSdellines(w, top, bottom, n, scrolled);
  121.     return(0);                /* I delete the whole thing! */
  122.   } /* VSIcdellines */
  123.  
  124. VSIcinslines(w, top, bottom, n,scrolled)
  125.     int
  126.         w, /* affected window */
  127.         top, /* where to insert blank lines */
  128.         bottom, /* bottom of area to scroll */
  129.         n, /* number of lines to insert */
  130.         scrolled; /* -ve <=> cancel current selection, if any */
  131.   /* updates the display to indicate insertion of the specified
  132.     number of blank lines at the top of the specified region.
  133.     Returns 0 iff any part of the change is visible. */
  134.   {
  135.     int
  136.         x1 = 0,
  137.         x2 = VSIw->maxwidth,
  138.         tn = -1,
  139.         offset;
  140.  
  141.     if (VSIclip(&x1, &top, &x2, &bottom, &tn, &offset))
  142.         return -1; /* affected region is invisible */
  143.     tn = bottom - top;
  144.     if (tn < n)
  145.         n = tn; /* don't bother scrolling more lines than scrolling region holds */
  146.     RSinslines(w, top, bottom, n, scrolled);
  147.     return 0;
  148.   } /* VSIcinslines */
  149.  
  150. void VSIcurson
  151.   (
  152.     int w,
  153.     int x,
  154.     int y,
  155.     int ForceMove
  156.   )
  157.   /* displays the text cursor at the specified position. If
  158.     ForceMove is true, I am to do any appropriate scrolling of
  159.     the display to ensure the cursor is within the visible region.
  160.     Assumes cursor isn't currently being shown. */
  161.   {
  162.     int
  163.         x2,
  164.         y2,
  165.         n = 1,
  166.         offset;
  167.  
  168.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))
  169.       /* cursor already lies within visible region */
  170.         RScurson(w, x, y); /* just make it visible */
  171.     else if (ForceMove)
  172.       {
  173.       /* scroll to make cursor visible */
  174.         x2 = VSIw->Rbottom - VSIw->Rtop;
  175.         if (x2 >= VSIw->lines)
  176.           /* visible region is big enough to show entire screen--
  177.             make sure I don't scroll off the bottom of the screen.
  178.             This call will also do any appropriate scrolling and
  179.             redisplaying of the cursor. */
  180.             VSsetrgn(VSIwn, VSIw->Rleft, VSIw->lines - x2,
  181.                 VSIw->Rright, VSIw->lines);
  182.         else
  183.           {
  184.           /* x & y have been normalized relative to left & top
  185.             of current visible region. Just call the appropriate scroll
  186.             routine, which will also redisplay the cursor. */
  187.             if (y > 0)
  188.                 VSscrolforward(VSIwn, y);
  189.             else
  190.                 VSscrolback(VSIwn, -y);
  191.           } /* if */
  192.       } /* if */
  193.   } /* VSIcurson */
  194.  
  195. void VSIcuroff
  196.   (
  197.     int w
  198.   )
  199.   /* hides the cursor for the specified screen. Assumes it
  200.     is currently being shown (or that it's on an invisible
  201.     part of the screen). */
  202.   {
  203.     int
  204.         x = VSIw->x,
  205.         y = VSIw->y,
  206.         x2,
  207.         y2,
  208.         n = 1,
  209.         offset;
  210.  
  211.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))
  212.       /* cursor is on visible part of screen */
  213.         RScursoff(w);
  214.   } /* VSIcuroff */
  215.  
  216. int VSIcursorvisible        /* BYU 2.4.12 */
  217.   (                            /* BYU 2.4.12 */
  218.     void                    /* BYU 2.4.12 */
  219.   )                            /* BYU 2.4.12 */
  220.   {                            /* BYU 2.4.12 */
  221.     int                        /* BYU 2.4.12 */
  222.         x = VSIw->x,        /* BYU 2.4.12 */
  223.         y = VSIw->y,        /* BYU 2.4.12 */
  224.         x2,                    /* BYU 2.4.12 */
  225.         y2,                    /* BYU 2.4.12 */
  226.         n = 1,                /* BYU 2.4.12 */
  227.         offset;                /* BYU 2.4.12 */
  228.                                                             /* BYU 2.4.12 */
  229.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))            /* BYU 2.4.12 */
  230.         return 1;                                            /* BYU 2.4.12 */
  231.     else                    /* BYU 2.4.12 */
  232.         return 0;            /* BYU 2.4.12 */
  233. }                            /* BYU 2.4.12 */
  234.  
  235. VSlineArray VSInewlinearray
  236.   (
  237.     int nrlines
  238.   )
  239.   /* allocates an array to hold the specified number of pointers
  240.     to line elements. */
  241.   {
  242.     return
  243.         (VSlineArray) NewPtr(sizeof(VSlinePtr) * nrlines);
  244.   } /* VSInewlinearray */
  245.  
  246. VSlinePtr VSInewlines
  247.   (
  248.     int nlines
  249.   )
  250.   /* allocates a doubly-linked list of the specified number of
  251.     line elements, and returns a pointer to the head of the list,
  252.     or nil if ran out of memory. The maximum number of characters
  253.     each line can hold is controlled by VSIw->allwidth. */
  254.   {
  255.     VSlinePtr t2;
  256.     char *t;
  257.     register int i;
  258.     
  259.     if (!RSokmem(30000))                /* don't use up end of mem for scrollback */
  260.         return((VSlinePtr) 0L);
  261.   /* allocate one block for the line list elements, and another
  262.     block for the line contents. These blocks will be divided up
  263.     and appropriate flags set so I will be able to call DisposPtr
  264.     the right number of times, with the right arguments. */
  265.     if ((t = NewPtr(nlines * (VSIw->allwidth + 1))) != 0L)
  266.       {
  267.         if ((t2 = (VSlinePtr) NewPtr(nlines * sizeof(VSline))) != 0L)
  268.             t2->text = t;
  269.         else
  270.           {
  271.           /* clean up gracefully before giving up */
  272.             DisposPtr(t);
  273.             return(0L);
  274.           } /* if */
  275.       }
  276.     else
  277.       /* sorree no memoree */
  278.         return((VSlinePtr) 0L);
  279.  
  280. /*
  281. *  indicate to the free routine that the first record is the one to free.
  282. */
  283.     t2->mem = 1;                        /* call DisposPtr on this one */
  284.     t2->next = t2 + 1;                    /* point to next one */
  285. /*
  286. *  Take our allocation for multiple lines and fill in the structures to 
  287. *  point to the right text fields and connect the doubly-linked chain.
  288. *
  289. */
  290.     for (i = 1; i < nlines; i++)
  291.       {
  292.         t += (VSIw->allwidth + 1);        /* inc to next text space for a line */
  293.         t2[i].mem = 0;                    /* don't DisposPtr any of these */
  294.         t2[i].text = t;
  295.         t2[i].prev = t2 + i - 1;        /* point back one */
  296.         t2[i].next = t2 + i + 1;        /* point forward one */
  297.       } /* for */
  298.     
  299.     t2[0].prev = 0L;                    /* first one has no prev yet */
  300.     t2[nlines - 1].next = 0L;            /* last one has no next yet */
  301.  
  302.     return(t2);
  303.   } /* VSInewlines */
  304.  
  305. void VSIlistndx
  306.   (
  307.     register VSlinePtr ts,
  308.     register VSlinePtr as
  309.   )
  310.   /* sets up the screen arrays for the current screen to point
  311.     at the given lists of attribute and text lines. */
  312.   {
  313.     register int i;
  314.     for (i = 0; i <= VSIw->lines; i++)
  315.       {
  316.         VSIw->attrst[i] = as;
  317.         VSIw->linest[i] = ts;
  318.         ts = ts->next;
  319.         as = as->next;
  320.       } /* for */
  321.   } /* VSIlistndx */
  322.  
  323. void VSIscroff
  324.   (
  325.     void
  326.   )
  327.   /* called to save current screen contents in scrollback buffer,
  328.     if it is ordained that I should do so. This is called by VSIes
  329.     (below) just before the entire screen is cleared. */
  330.   {
  331.     VSlinePtr tmp;
  332.     register int i, j;
  333.  
  334.     if
  335.       (
  336.             (!VSIw->savelines) /* not saving lines */
  337.         ||
  338.             (VSIw->top != 0) || (VSIw->bottom != VSIw->lines)
  339.               /* scrolling region isn't exactly the entire screen */
  340.       )
  341.         return; /* do nothing */
  342.  
  343.     tmp = VSIw->linest[VSIw->lines]; /* need VSIw->lines + 1 more lines */
  344.     for (i = 0; i <= VSIw->lines; i++)
  345.       {
  346.       /* count off the lines in the part of the scrollback buffer
  347.         below the screen (if any), to see if there's enough to hold
  348.         a screenful. If the scrollback list isn't circular, then
  349.         this part contains lines that have been allocated, but not
  350.         yet used. If the list is circular (meaning it has reached
  351.         its full size), then this is the part that is next in line
  352.         for reuse. */
  353.         if (!tmp->next) 
  354.           { /* not enough */
  355.             j = VSIw->maxlines - VSIw->numlines - i; /* potential unallocated scrollback */
  356.             if (j > ScrollbackQuantum)
  357.                 j = ScrollbackQuantum; /* but don't bother allocating more than this */
  358.             if (j <= 0)
  359.               {
  360.               /* already reached user-specified scrollback limit-- */
  361.               /* make the list circular to indicate no more extension. */
  362.                 tmp->next = VSIw->buftop;
  363.                 VSIw->buftop->prev = tmp;        /* connect it up */
  364.               }
  365.             else
  366.               {
  367.               /* extend the scrollback buffer to make room for
  368.                 another screenful */
  369.                 if (j < VSIw->lines - i + 1)
  370.                     j = VSIw->lines - i + 1; /* need at least this many */
  371.                 if ((tmp->next = VSInewlines(j)) != 0L) 
  372.                     tmp->next->prev = tmp;        /* got some space--link it up */
  373.                 else
  374.                   {
  375.                   /* out of memory--no more extensions */
  376.                     tmp->next = VSIw->buftop;
  377.                     VSIw->buftop->prev = tmp;    
  378.                   } /* if */                
  379.               } /* if */
  380.             break;                                /* only allocate once is enough */
  381.           } /* if */
  382.         tmp = tmp->next; /* keep counting */
  383.       } /* for */
  384.         
  385. /*
  386. *  at this point, we know we have enough memory for the whole scroll.
  387. *  It might be wraparound (reuse of some line elements), might not.
  388. */
  389.         
  390.     for (i = 0; i <= VSIw->lines; i++)
  391.       {
  392.       /* push another screen line into the scrollback area */
  393.         if (VSIw->linest[VSIw->lines]->next == VSIw->buftop)
  394.             VSIw->buftop = VSIw->buftop->next;    /* reusing old space */
  395.         else
  396.             VSIw->numlines++;                /* using some new space */
  397.         VSIw->scrntop = VSIw->scrntop->next; /* move another line into the scrollback buffer */
  398.         VSIlistndx(VSIw->scrntop, VSIw->attrst[1]); /* and update screen arrays */
  399.       /* note that it's up to the caller to clear out the new screen text
  400.         and attribute lines */
  401.       } /* for */
  402.  
  403.     VSIw->vistop = VSIw->scrntop;
  404.     RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar */
  405.   } /* VSIscroff */
  406.  
  407. void VSIelo
  408.   (
  409.     int s /* line to erase, -ve => line containing cursor */
  410.   )
  411.   /* blanks out the specified line in the screen buffer.
  412.     Doesn't do anything to the display. */
  413.   {
  414.     char *tt, *ta;
  415.     int i;
  416.  
  417.     if (s < 0)
  418.         s = VSIw->y;
  419.  
  420.     ta = &VSIw->attrst[s]->text[0];
  421.     tt = &VSIw->linest[s]->text[0];
  422.     for (i = 0; i <= VSIw->allwidth; i++)
  423.       {
  424.         *ta++ = VSIclrattrib;
  425.         *tt++ = ' ';
  426.       } /* for */
  427.   } /* VSIelo */
  428.  
  429. void VSIes
  430.   (
  431.     void
  432.   )
  433.   /* clears the screen, first saving its contents in the
  434.     scrollback buffer if appropriate. Also updates the display. */
  435.   {
  436.     int
  437.         i;
  438.     int
  439.         x1 = 0,
  440.         y1 = 0,
  441.         x2 = VSIw->maxwidth,
  442.         y2 = VSIw->lines,
  443.         n = -1,
  444.         offset;
  445.  
  446.   /* save screen contents in scrollback buffer, if appropriate */
  447.     if (VSIw->ESscroll)
  448.         VSIscroff();
  449.   /* clear out screen buffer */
  450.     for (i = 0; i <= VSIw->lines; i++)
  451.         VSIelo(i);
  452.   /* update display to show what I've done */
  453.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  454.         RSerase(VSIwn, x1, y1, x2, y2);
  455.     VSIw->vistop = VSIw->scrntop;
  456.   } /* VSIes */
  457.  
  458. void VSItabclear
  459.   (
  460.     void
  461.   )
  462.   /* clears all current tab settings. */
  463.   {
  464.     int
  465.         x = 0;
  466.  
  467.     while (x <= VSIw->allwidth)
  468.       {
  469.         VSIw->tabs[x] = ' ';
  470.         x++;
  471.       } /* while */
  472.   } /* VSItabclear */
  473.  
  474. void VSItabinit
  475.   (
  476.     void
  477.   )
  478.   /* sets tab settings to default (every 8 columns). */
  479.   {
  480.     int
  481.         x = 0;
  482.  
  483.     VSItabclear();
  484.     while (x <= VSIw->allwidth)
  485.       {
  486.         VSIw->tabs[x] = 'x';
  487.         x += 8;
  488.       }
  489.     VSIw->tabs[VSIw->allwidth] = 'x';
  490.   } /* VSItabinit */
  491.  
  492. void VSIreset
  493.   (
  494.     void
  495.   )
  496.   /* restores terminal mode settings to defaults and clears screen. */
  497.   {
  498.     VSIw->top = 0;
  499.     VSIw->bottom = VSIw->lines;
  500.     VSIw->parmptr = 0;
  501.     VSIw->escflg = 0;
  502.     VSIw->DECAWM = 0;
  503.     VSIw->DECCKM = 0;
  504.     VSIw->DECPAM = 0;
  505.     VSIw->DECORG = 0;
  506.     VSIw->IRM = 0;
  507.     VSIw->attrib = 0;
  508.     VSIw->Pattrib = -1;
  509.     VSIw->x = 0;
  510.     VSIw->y = 0;
  511.     VSIw->charset = 0;
  512.     VSIes();
  513.     VSItabinit();
  514.   } /* VSIreset */
  515.  
  516. VSIlistmove(TD, BD, TI, BI)
  517.     register VSlinePtr TD, BD, TI, BI;
  518.   /* moves the lines from TD to BD inclusive from their
  519.     present position to between TI and BI. Either of the
  520.     latter may be nil. */
  521.   {
  522.   /* unlink the lines from TD to BD */
  523.     if (TD->prev != 0L)
  524.         TD->prev->next = BD->next;    /* Maintain circularity */
  525.     if (BD->next != 0L)
  526.         BD->next->prev = TD->prev;
  527.   /* relink them between TI and BI */
  528.     TD->prev = TI;                                /* Place the node in its new home */
  529.     BD->next = BI;
  530.     if (TI != 0L)
  531.         TI->next = TD;                    /* Ditto prev->prev */
  532.     if (BI != 0L)
  533.         BI->prev = BD;
  534.   } /* VSIlistmove */
  535.  
  536. void VSIdellines
  537.   (
  538.     int n, /* nr lines to delete */
  539.     int s /* starting line to delete, -ve => line containing cursor */
  540.   )
  541.   /* deletes lines from the screen, scrolling up the remainder and
  542.     inserting new blank lines at the bottom of the scrolling region. */
  543.   {
  544.     int i, j;
  545.     char *ta, *tt;
  546.     VSlinePtr as, ts, TD, BD, TI, BI, itt, ita;
  547.  
  548.     if (s < 0)
  549.         s = VSIw->y;
  550.     if (s + n - 1 > VSIw->bottom)
  551.         n = VSIw->bottom - s + 1; /* don't bother deleting more than scrolling region will hold */
  552.  
  553.   /* find new tops of screen arrays */
  554.     if (s == 0 && n <= VSIw->lines)
  555.       {
  556.       /* element for line after last one being deleted */
  557.         ts = VSIw->linest[n];
  558.         as = VSIw->attrst[n];
  559.       }
  560.     else
  561.       {
  562.       /* top line unaffected, or entire screen is being wiped */
  563.         ts = VSIw->linest[0];
  564.         as = VSIw->attrst[0];
  565.       } /* if */
  566.  
  567.     TD = VSIw->linest[s]; /* topmost line to delete */
  568.     BD = VSIw->linest[s + n - 1]; /* bottommost line to delete */
  569.     TI = VSIw->linest[VSIw->bottom]; /* insert replacement blank lines after this line */
  570.     BI = TI->next; /* insert them before this line (might be nil) */
  571.     itt = TD; /* start of text lines to be blanked out */
  572.   /* the space taken by the deleted lines will be reused for
  573.     the inserted blank lines */
  574.     if (TD != BI && TI != BD)
  575.       /* insertion and deletion areas not adjacent -- move the lines to
  576.         their new position */
  577.         VSIlistmove(TD, BD, TI, BI);
  578.  
  579.     TD = VSIw->attrst[s]; /* topmost line to delete */
  580.     BD = VSIw->attrst[s + n - 1]; /* bottommost line to delete */
  581.     TI = VSIw->attrst[VSIw->bottom]; /* insert new lines after this one */
  582.     BI = TI->next; /* insert them before this line */
  583.   /* perform same rearrangement on attribute lines as on text lines */
  584.     if (TD != BI && TI != BD)
  585.       /* insertion and deletion areas not adjacent -- move the lines to
  586.         their new position */
  587.         VSIlistmove(TD, BD, TI, BI);
  588.  
  589.   /* blank out the newly-created replacement lines */
  590.     ita = TD; /* start of attribute lines to be blanked out */
  591.     for (i = 0; i < n; i++)
  592.       {
  593.         ta = ita->text;
  594.         tt = itt->text;
  595.         for (j = 0; j <= VSIw->allwidth; j++)
  596.           {
  597.             *tt++ = ' ';
  598.             *ta++ = VSIclrattrib;
  599.           } /* for */
  600.         ita = ita->next;
  601.         itt = itt->next;
  602.       } /* for */
  603.  
  604.     VSIw->scrntop = ts; /* new topmost line (if it's changed) */
  605.   /* re-sync screen arrays */
  606.     VSIlistndx(ts, as);
  607.     if (VSIw->Rtop >= 0)
  608.       /* make sure vistop still points to same line position
  609.         on screen that it did before */
  610.         VSIw->vistop = VSIw->linest[VSIw->Rtop];
  611.   /* and actually display the change on-screen */
  612.     VSIcdellines(VSIwn, s, VSIw->bottom, n, -1); /* Cancel current selection */
  613.   } /* VSIdellines */
  614.  
  615. void VSIinslines
  616.   (
  617.     int n, /* how many to insert */
  618.     int s /* where to insert them, -ve => line containing cursor */
  619.   )
  620.   /* inserts the specified number of blank lines, scrolling the
  621.     remaining ones down, and dropping off any that fall off the
  622.     end of the scrolling region. */
  623.   {
  624.     int i, j;
  625.     char *ta, *tt;
  626.     VSlinePtr as, ts, TD, BD, TI, BI, itt, ita;
  627.  
  628.     if (s < 0)
  629.         s = VSIw->y;
  630.     if (s + n - 1 > VSIw->bottom)
  631.       /* don't bother inserting more than scrolling region can hold */
  632.         n = VSIw->bottom - s + 1;
  633.  
  634.   /* find new tops of screen arrays */
  635.     if (s == 0 && n <= VSIw->lines)
  636.       {
  637.       /* element for first blank line being inserted */
  638.         ts = VSIw->linest[VSIw->bottom - n + 1];
  639.         as = VSIw->attrst[VSIw->bottom - n + 1];
  640.       }
  641.     else
  642.       {
  643.       /* top line unaffected, or entire screen is being wiped */
  644.         ts = VSIw->linest[0];
  645.         as = VSIw->attrst[0];
  646.       } /* if */
  647.  
  648.     BI = VSIw->linest[s]; /* insert blank lines before this one */
  649.     TI = BI->prev; /* insert them after this one */
  650.     TD = VSIw->linest[VSIw->bottom - n + 1]; /* topmost line to delete */
  651.     BD = VSIw->linest[VSIw->bottom]; /* bottommost line to delete */
  652.     itt = TD; /* start of text lines to be blanked out */
  653.   /* the space occupied by the deleted lines will be reused for
  654.     the new blank lines */
  655.     if (TD != BI && TI != BD)
  656.       /* new and deleted lines not contiguous -- move the space
  657.         to its new position */
  658.         VSIlistmove(TD, BD, TI, BI);
  659.  
  660.     BI = VSIw->attrst[s]; /* insert new lines before this one */
  661.     TI = BI->prev; /* insert them after this one */
  662.     TD = VSIw->attrst[VSIw->bottom - n + 1]; /* topmost line to delete */
  663.     BD = VSIw->attrst[VSIw->bottom]; /* bottommost line to delete */
  664.   /* do the same rearrangement on the attribute lines */
  665.     if (TD != BI && TI != BD)
  666.       /* new and deleted lines not contiguous -- move the space
  667.         to its new position */
  668.         VSIlistmove(TD, BD, TI, BI);
  669.  
  670.   /* blank out the newly-inserted lines */
  671.     ita = TD; /* start of attribute lines to be blanked out */
  672.     for (i = 0; i < n; i++)
  673.       {
  674.         tt = itt->text;
  675.         ta = ita->text;
  676.         for (j = 0; j <= VSIw->allwidth; j++)
  677.           {
  678.             *tt++ = ' ';
  679.             *ta++ = VSIclrattrib;
  680.           }
  681.         itt = itt->next;
  682.         ita = ita->next;
  683.       } /* for */
  684.  
  685.     VSIw->scrntop = ts;
  686.     VSIlistndx(ts, as); /* re-sync screen arrays */
  687.     if (VSIw->Rtop >= 0)
  688.       /* make sure vistop still points to same line position
  689.         on screen that it did before */
  690.         VSIw->vistop = VSIw->linest[VSIw->Rtop];
  691.   /* update display to match reality */
  692.     VSIcinslines(VSIwn, s, VSIw->bottom, n, -1);  /* Destroy selection area if this is called tooo */
  693.   } /* VSIinslines */
  694.  
  695. void VSIscroll
  696.   (
  697.     void
  698.   )
  699.   /* scrolls scrolling region up one line. */
  700.   {
  701.     register char *temp, *tempa;
  702.     VSlinePtr tmp;
  703.     register int i;
  704.     int tx1, tx2, ty1, ty2, tn, offset;
  705.  
  706.     if (VSIw->y > VSIw->lines)        /* BYU - replaces BYU modification below */
  707.         return;                        /* BYU */
  708.  
  709.     tx1 = ty1 = 0;
  710.     tn = 132;
  711.     if (!VSIclip(&tx1, &ty1, &tx2, &ty2, &tn, &offset))
  712.       /* top line of screen is visible */
  713.         RSdrawsep(VSIwn, ty1, 1);                    /* Draw Separator */
  714.  
  715. #if 0                                                        /* BYU */
  716. /* BYU mod - Don't scroll if the cursor is outside of the scrolling region */
  717.     if ((VSIw->y < VSIw->top) || (VSIw->y > VSIw->bottom))    /* BYU */
  718.         return;                                                /* BYU */
  719. #endif                                                        /* BYU */
  720.  
  721.     if
  722.       (
  723.             (!VSIw->savelines) /* no scrollback */
  724.         ||
  725.             (VSIw->top != 0) || (VSIw->bottom != VSIw->lines)
  726.               /* region being scrolled is not entire screen */
  727.       )
  728.       /* no saving of lines */
  729.         VSIdellines(1, VSIw->top);
  730.     else
  731.       {
  732.       /* scrolling region is entire screen, and lines are being saved off top */
  733.         if (VSIw->linest[VSIw->lines]->next == 0L)
  734.           {
  735.           /* all currently-allocated scrollback lines have been used, but
  736.             scrollback buffer isn't at its full size -- allocate some more
  737.             space */
  738.             i = VSIw->maxlines - VSIw->numlines; /* number of lines that can be allocated */
  739.             if (i > ScrollbackQuantum)
  740.                 i = ScrollbackQuantum; /* don't bother allocating more than this at once */
  741.             if ((i > 0) && (tmp = VSInewlines(i)) != 0L)
  742.               {
  743.               /* link newly-allocated lines into the list */
  744.                 VSIw->linest[VSIw->lines]->next = tmp;
  745.                 tmp->prev = VSIw->linest[VSIw->lines];
  746.                 VSIw->numlines++; /* use one of the newly-allocated scrollback lines */
  747.                 RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar accordingly */
  748.               }
  749.             else
  750.               {
  751.               /* not enough memory to extend scrollback buffer--reuse
  752.                 oldest line and give up on future extensions */
  753.                 VSIw->linest[VSIw->lines]->next = VSIw->buftop;        /* Make it circular */
  754.                 VSIw->buftop->prev = VSIw->linest[VSIw->lines];
  755.                 VSIw->buftop = VSIw->buftop->next;    /* step one forward */
  756.               } /* if */
  757.           }    
  758.         else
  759.           {
  760.           /* either there's allocated, but not yet used, space at
  761.             VSIw->linest[VSIw->lines]->next, or the text line list
  762.             is circular. Either way, don't do any new scrollback
  763.             allocation. */
  764.             if (VSIw->linest[VSIw->lines]->next == VSIw->buftop)
  765.               /* scrollback buffer is at full size--reuse oldest line */
  766.                 VSIw->buftop = VSIw->buftop->next;
  767.             else
  768.               {
  769.               /* haven't used up all the space I allocated last time */
  770.                 VSIw->numlines++;                    /* count another line */
  771.                 RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar accordingly */
  772.               } /* if */
  773.           } /* if */
  774.  
  775.         VSIw->scrntop = VSIw->scrntop->next; /* scroll the screen buffer */
  776.         VSIlistndx(VSIw->scrntop, VSIw->attrst[1]); /* update screen arrays */
  777.       /* reflect the change in the display by scrolling up the visible
  778.         part of the on-screen area, if any */
  779.         if (VSIcdellines(VSIwn, VSIw->Rtop, VSIw->Rbottom, 1, 1))
  780.           {
  781.           /* no part of on-screen area is visible */
  782.             if (VSIw->Rtop > -VSIw->numlines)
  783.               /* update bounds of visible region to be consistent
  784.                 with portion of scrollback buffer still being displayed */
  785.               {
  786.                 VSIw->Rtop--;
  787.                 VSIw->Rbottom--;
  788.               }
  789.             else
  790.               {
  791.               /* displaying right from top of scrollback buffer. Topmost
  792.                 line being shown has in fact vanished. Update the display
  793.                 to show this fact. */
  794.                 VSIw->vistop = VSIw->vistop->next;
  795.                 RSdellines(VSIwn, 0, VSIw->Rbottom - VSIw->Rtop, 1, 1);
  796.               } /* if */
  797.           }
  798.         else
  799.             VSIw->vistop = VSIw->vistop->next; /* consistent with changed display */
  800.       /* blank out newly-revealed bottom line */
  801.         tempa = VSIw->attrst[VSIw->lines]->text;
  802.         temp = VSIw->linest[VSIw->lines]->text;
  803.         for (i = 0; i <= VSIw->allwidth; i++)
  804.           {
  805.             *temp++ = ' ';
  806.             *tempa++ = 0;
  807.           } /* for */
  808.       } /* if */
  809.     tx1 = ty1 = 0;
  810.     tn = 132;
  811.     if (!VSIclip(&tx1, &ty1, &tx2, &ty2, &tn, &offset))
  812.         RSdrawsep(VSIwn, ty1, 1);                    /* Draw Separator */
  813.   } /* VSIscroll */
  814.  
  815. void VSIindex
  816.   (
  817.     void
  818.   )
  819.   /* moves cursor down one line, unless it's at the bottom of
  820.     the scrolling region, in which case scrolls up one. */
  821.   {
  822.     if (VSIw->y == VSIw->bottom)    /* BYU - changed "==" to ">=" and back again */
  823.         VSIscroll();
  824.     else if (VSIw->y < VSIw->lines)     /* BYU  - added "if ... " */
  825.         VSIw->y++;
  826.   } /* VSIindex */
  827.  
  828. VSIwrapnow(xp, yp)
  829.     int *xp, *yp;
  830.   /* checks current cursor position for VSIw to see if
  831.     it's within bounds, wrapping to next line if not.
  832.     Returns correct cursor position in either case in *xp
  833.     and *yp. */
  834.   {
  835.     if (VSIw->x > VSIw->maxwidth) 
  836.       {
  837.         VSIw->x = 0;
  838.         VSIindex();
  839.       } /* if */
  840.     *xp = VSIw->x;
  841.     *yp = VSIw->y;
  842.   } /* VSIwrapnow */
  843.  
  844. void VSIeeol
  845.   (
  846.     void
  847.   )
  848.   /* erases characters to the end of the current line. */
  849.   {
  850.     char
  851.         *tt,
  852.         *ta;
  853.     int
  854.         x1 = VSIw->x,
  855.         y1 = VSIw->y,
  856.         x2 = VSIw->maxwidth,
  857.         y2 = VSIw->y,
  858.         n = -1,
  859.         offset;
  860.     int
  861.         i;
  862.  
  863.     VSIwrapnow(&x1, &y1);
  864.     y2 = y1;
  865.   /* clear out screen line */
  866.     ta = &VSIw->attrst[y1]->text[x1];
  867.     tt = &VSIw->linest[y1]->text[x1];
  868.     for (i = VSIw->allwidth - x1 + 1; i > 0; i--)
  869.       {
  870.         *ta++ = VSIclrattrib;
  871.         *tt++ = ' ';
  872.       }
  873.   /* update display */
  874.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  875.         RSerase(VSIwn, x1, y1, x2, y2);
  876.   } /* VSIeeol */
  877.  
  878. void VSIdelchars
  879.   (
  880.     int x /* how many characters to delete */
  881.   )
  882.   /* deletes characters at the current cursor position onwards,
  883.     moving the remainder of the line to the left. */
  884.   {
  885.     int
  886.         i;
  887.     int
  888.         x1 = VSIw->x,
  889.         y1 = VSIw->y,
  890.         x2 = VSIw->maxwidth,
  891.         y2 = VSIw->y,
  892.         n = -1,
  893.         offset;
  894.     char
  895.         *tempa,
  896.         *temp;
  897.  
  898.     VSIwrapnow(&x1, &y1);
  899.     y2 = y1;
  900.  
  901.     if (x > VSIw->maxwidth)
  902.         x = VSIw->maxwidth;
  903.     tempa = VSIw->attrst[y1]->text;
  904.     temp = VSIw->linest[y1]->text;
  905.     for (i = x1; i <= VSIw->maxwidth - x; i++)
  906.       {
  907.       /* move remainder of line to the left */
  908.         temp[i] = temp[x + i];
  909.         tempa[i] = tempa[x + i];
  910.       }
  911.     for (i = VSIw->maxwidth - x + 1; i <= VSIw->allwidth; i++)
  912.       {
  913.       /* insert blank characters after end of line */
  914.         temp[i] = ' ';
  915.         tempa[i] = VSIclrattrib;
  916.       }
  917.   /* update display */
  918.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset))
  919.       {
  920.         if (VSIw->VSIDC)
  921.             RSdelchars(VSIwn, x1, y1, x);
  922.         else
  923.           /* redraw from cursor position to end of line.
  924.             Trouble is, this isn't going to preserve the
  925.             right attributes. */
  926.             RSdraw
  927.               (
  928.                 VSIwn, x1, y1, VSIw->attrib, n,
  929.                 &VSIw->linest[y1]->text[x1]
  930.               );
  931.       } /* if */
  932.   } /* VSIdelchars */
  933.  
  934. void VSIfreelinelist
  935.   (
  936.     VSlinePtr listhead
  937.   )
  938.   /* frees up the list of line elements pointed to by listhead. */
  939.   {
  940.     register VSlinePtr
  941.         ThisElt, NextElt, ToFree;
  942.  
  943.     ThisElt = listhead;
  944.     ToFree = nil;
  945.     while (true)
  946.       {
  947.         if (ThisElt == nil)
  948.             break;
  949.         NextElt = ThisElt->next;
  950.         if (ThisElt->mem)
  951.           {
  952.             ThisElt->next = ToFree;
  953.             ToFree = ThisElt;
  954.           } /* if */
  955.         ThisElt = NextElt;
  956.         if (ThisElt == listhead)
  957.             break;
  958.       } /* while */
  959.     while (ToFree)
  960.       {
  961.         NextElt = ToFree->next;
  962.         DisposPtr(ToFree->text);
  963.         DisposPtr((Ptr) ToFree);
  964.         ToFree = NextElt;
  965.       } /* while */
  966.   } /* VSIfreelinelist */
  967.  
  968. void VSIfreelines
  969.   (
  970.     void
  971.   )
  972.   /* frees up all the memory allocated for screen and scrollback
  973.     text lines for the current screen. */
  974.   {
  975.     VSIfreelinelist(VSIw->buftop);
  976.   } /* VSIfreelines */
  977.  
  978. void VSIrindex
  979.   (
  980.     void
  981.   )
  982.   /* moves cursor up one line, unless it's at the top of
  983.     the scrolling region, in which case scrolls down one. */
  984.   {
  985.     if (VSIw->y == VSIw->top)
  986.         VSIinslines(1, VSIw->top);
  987.     else
  988.         VSIw->y--;
  989.   } /* VSIrindex */
  990.  
  991. void VSIebol
  992.   (
  993.     void
  994.   )
  995.   /* erases characters from beginning of line to cursor. */
  996.   {
  997.     char
  998.         *tt,
  999.         *ta;
  1000.     int
  1001.         x1 = 0,
  1002.         y1 = VSIw->y,
  1003.         x2 = VSIw->x,
  1004.         y2 = VSIw->y,
  1005.         n = -1,
  1006.         offset;
  1007.     int
  1008.         i;
  1009.  
  1010.     VSIwrapnow(&x2, &y1);
  1011.     y2 = y1;
  1012.   /* clear from beginning of line to cursor */
  1013.     ta = &VSIw->attrst[y1]->text[0];
  1014.     tt = &VSIw->linest[y1]->text[0];
  1015.     for (i = 0; i <= x2; i++)
  1016.       {
  1017.         *ta++ = VSIclrattrib;
  1018.         *tt++ = ' ';
  1019.       }
  1020.   /* update display */
  1021.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1022.         RSerase(VSIwn, x1, y1, x2, y2);
  1023.   } /* VSIebol */
  1024.  
  1025. void VSIel
  1026.   (
  1027.     int s /* line to clear, -ve => line containing cursor */
  1028.   )
  1029.   /* erases the specified line. */
  1030.   {
  1031.     char
  1032.         *tt,
  1033.         *ta;
  1034.     int
  1035.         x1 = 0,
  1036.         y1 = s,
  1037.         x2 = VSIw->maxwidth,
  1038.         y2 = s,
  1039.         n = -1,
  1040.         offset;
  1041.     int
  1042.         i;
  1043.  
  1044.     if (s < 0)
  1045.       {
  1046.         VSIwrapnow(&x1, &y1);
  1047.         s = y2 = y1;
  1048.         x1 = 0;
  1049.       } /* if */
  1050.   /* clear out line */
  1051.     ta = &VSIw->attrst[s]->text[0];
  1052.     tt = &VSIw->linest[s]->text[0];
  1053.     for(i = 0; i <= VSIw->allwidth; i++)
  1054.       {
  1055.         *ta++ = VSIclrattrib;
  1056.         *tt++ = ' ';
  1057.       }
  1058.   /* update display */
  1059.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1060.         RSerase(VSIwn, x1, y1, x2, y2);
  1061.   } /* VSIel */
  1062.  
  1063. void VSIeeos
  1064.   (
  1065.     void
  1066.   )
  1067.   /* erases characters from cursor to end of screen. */
  1068.   {
  1069.     int
  1070.         i;
  1071.     int
  1072.         x1 = 0,
  1073.         y1 = VSIw->y + 1,
  1074.         x2 = VSIw->maxwidth,
  1075.         y2 = VSIw->lines,
  1076.         n = -1,
  1077.         offset;
  1078.  
  1079.     VSIwrapnow(&x1, &y1);
  1080.     y1++;
  1081.     x1 = 0;
  1082.  
  1083.     i = y1;
  1084.   /* erase complete lines from screen */
  1085.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1086.         RSerase(VSIwn, x1, y1, x2, y2);
  1087.   /* blank out current line from cursor to end */
  1088.     VSIeeol(); /* this also erases the partial line on-screen */
  1089.   /* blank out remaining lines to end of screen */
  1090.     while (i <= VSIw->lines)
  1091.       {
  1092.         VSIelo(i);
  1093.         i++;
  1094.       } /* while */
  1095.     if (VSIw->y < VSIw->lines && (VSIw->x <= VSIw->maxwidth))
  1096.       /* erase the partial line (what--again??) */
  1097.         if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1098.             RSerase(VSIwn, x1, y1, x2, y2);
  1099.   } /* VSIeeos */
  1100.  
  1101. void VSIebos
  1102.   (
  1103.     void
  1104.   )
  1105.   /* erases characters from beginning of screen to cursor. */
  1106.   {
  1107.     int
  1108.         i;
  1109.     int
  1110.         x1,
  1111.         y1,
  1112.         x2 = VSIw->maxwidth,
  1113.         y2,
  1114.         n = -1,
  1115.         offset;
  1116.  
  1117.     VSIwrapnow(&x1, &y1);
  1118.     y2 = y1 - 1;
  1119.     x1 = 0;
  1120.     y1 = 0;
  1121.   /* blank out current line from beginning to cursor */
  1122.     VSIebol(); /* this also erases the partial line on-screen */
  1123.     i = 0;
  1124.   /* blank out remaining lines from beginning of screen to previous line */
  1125.     while (i < (y2 + 1))
  1126.       {
  1127.         VSIelo(i);
  1128.         i++;
  1129.       } /* while */
  1130.     if (y2 >= 0)
  1131.       /* erase the partial line (what--again??) */
  1132.         if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1133.             RSerase(VSIwn, x1, y1, x2, y2);
  1134.   } /* VSIebos */
  1135.  
  1136. void VSIrange
  1137.   (
  1138.     void
  1139.   )
  1140.   /* constrains cursor position to valid range (somewhere on the screen). */
  1141.   {
  1142.     int
  1143.         wrap = 0;
  1144.     if (VSIw->DECAWM)
  1145.         wrap = 1;
  1146.     if (VSIw->x < 0)
  1147.         VSIw->x = 0;
  1148.     if (VSIw->x > (VSIw->maxwidth + wrap))
  1149.         VSIw->x = VSIw->maxwidth + wrap;
  1150.     if (VSIw->y < 0)
  1151.         VSIw->y = 0;
  1152.     if (VSIw->y > VSIw->lines)
  1153.         VSIw->y = VSIw->lines;
  1154.   } /* VSIrange */
  1155.  
  1156. void VTsendpos
  1157.   (
  1158.     void
  1159.   )
  1160.   /* sends an escape sequence representing the current cursor position. */
  1161.   {
  1162.     char
  1163.         tempbuf[19];
  1164.     int
  1165.         x = VSIw->x,
  1166.         y = VSIw->y;
  1167.  
  1168.     if (x > VSIw->maxwidth)
  1169.       {
  1170.       /* autowrap pending */
  1171.         x = 0;
  1172.         y++;
  1173.       }
  1174.     if (y > VSIw->lines)
  1175.       /* scroll pending (because of the autowrap) */
  1176.         y = VSIw->lines;
  1177.  
  1178.     sprintf(tempbuf, "\033[%d;%dR", y + 1, x + 1);
  1179.     RSsendstring(VSIwn, tempbuf, strlen(tempbuf));
  1180.   } /* VTsendpos */
  1181.  
  1182. void VTsendstat
  1183.   (
  1184.     void
  1185.   )
  1186.   /* sends the terminal status string. */
  1187.   {
  1188.     RSsendstring(VSIwn, "\033[0n", 4);
  1189.   } /* VTsendstat */
  1190.  
  1191. void VTsendident
  1192.   (
  1193.     void
  1194.   )
  1195.   /* sends an appropriate terminal identification sequence. */
  1196.   {
  1197. #ifdef VT100RESP
  1198.     if (VSIw->allwidth > 80)
  1199.         RSsendstring(VSIwn, "\033[?4;6c", 7);
  1200.     else
  1201.         RSsendstring(VSIwn, "\033[?1;6c", 7);
  1202. #endif VT100RESP
  1203.     if (isEightBit(VSIwn) &&                                 /* BYU 2.4.12 */
  1204.         ((theWorld.keyBoardType == envAExtendKbd) ||        /* BYU 2.4.12 */
  1205.          (theWorld.keyBoardType == envExtISOADBKbd)))        /* BYU 2.4.12 */
  1206.         RSsendstring(VSIwn, "\033[?62;1;6c", 10);            /* BYU 2.4.12 - VT200-series*/
  1207.     else                                                    /* BYU 2.4.12 */
  1208.         RSsendstring(VSIwn, "\033[?6c", 5);                    /* BYU 2.4.12 - VT102 */
  1209.   } /* VTsendident */
  1210.  
  1211. void VTalign
  1212.   (
  1213.     void
  1214.   )
  1215.   /* fills screen with uppercase "E"s, for checking screen alignment. */
  1216.   /* Yeah, right. */
  1217.   {
  1218.     char *tt;
  1219.     int i, j;
  1220.  
  1221.     VSIes();        /* erase the screen */
  1222.     for (j = 0; j < VSIw->lines; j++)
  1223.       {
  1224.         tt = &VSIw->linest[j]->text[0];
  1225.         for (i = 0; i <= VSIw->maxwidth; i++)
  1226.             *tt++ = 'E';
  1227.       } /* for */
  1228.   /* update the display */
  1229.     VSredraw(VSIwn, 0, 0,
  1230.         (VSIw->Rright - VSIw->Rleft), (VSIw->Rbottom - VSIw->Rtop));
  1231.   } /* VTalign */
  1232.  
  1233. void VSIapclear
  1234.   (
  1235.     void
  1236.   )
  1237.   /* initializes all the parameters for the current control
  1238.     sequence, and the current param index, to zero. */
  1239.   {
  1240.     int
  1241.         parmptr = maxparms;
  1242.     while (--parmptr >= 0)
  1243.         VSIw->parms[parmptr] = -1;
  1244.     VSIw->parmptr = 0;
  1245.   } /* VSIapclear */
  1246.  
  1247. void VSIsetoption
  1248.   (
  1249.     int toggle /* 1 => set, 0 => reset */
  1250.   )
  1251.   /* sets/resets various options, as specified by the parms in
  1252.     the current control sequence. Note that this implementation
  1253.     will not set/reset more than one option at a time! */
  1254.   {
  1255.     int
  1256.         WindWidth = VSIw->Rright - VSIw->Rleft;
  1257.  
  1258.     switch (VSIw->parms[0])
  1259.       {
  1260.         case -2: /* DEC-private control sequence */
  1261.             switch (VSIw->parms[1])
  1262.               {
  1263.                 case 1: /* cursor-key mode */
  1264.                     VSIw->DECCKM = toggle;
  1265.                     break;
  1266.                 case 3: /* 80/132 columns */
  1267.                     VSIw->x = VSIw->y = 0; /* home cursor */
  1268.                     VSIes(); /* and clear screen */
  1269.                     if (toggle)        /* 132 column mode */
  1270.                         VSIw->maxwidth = VSIw->allwidth;
  1271.                     else
  1272.                         VSIw->maxwidth = 79;
  1273.                   /* update scroll bars */
  1274.                     RSmargininfo(VSIwn, VSIw->maxwidth - WindWidth, VSIw->Rleft);
  1275.                     break;
  1276.                 case 6: /* origin mode */
  1277.                     VSIw->DECORG = toggle;
  1278.                     break;
  1279.                 case 7: /* autowrap mode */
  1280.                     VSIw->DECAWM = toggle;
  1281.                     break;
  1282.                 default:
  1283.                     break;
  1284.               } /* switch */
  1285.             break;
  1286.         case  4: /* insert/replace character writing mode */
  1287.             VSIw->IRM = toggle;
  1288.             break;
  1289.         default:
  1290.             break;
  1291.       } /* switch */
  1292.   } /* VSIsetoption */
  1293.  
  1294. int VSItab
  1295.   (
  1296.     void
  1297.   )
  1298.   /* advances VSIw->x to the next tab position. */
  1299.   {
  1300.     if (VSIw->x >= VSIw->maxwidth)
  1301.       {
  1302.       /* already at right margin */
  1303.         VSIw->x = VSIw->maxwidth;
  1304.         return(0);
  1305.       } /* if */
  1306.     VSIw->x++; /* advance at least one position */
  1307.     while ((VSIw->tabs[VSIw->x] != 'x') && (VSIw->x < VSIw->maxwidth))
  1308.         VSIw->x++;
  1309.     return(0);
  1310.   } /* VSItab */
  1311.  
  1312. void VSIinschar
  1313.   (
  1314.     int x /* number of blanks to insert */
  1315.   )
  1316.   /* inserts the specified number of blank characters at the
  1317.     current cursor position, moving the rest of the line along,
  1318.     losing any characters that fall off the right margin.
  1319.     Does not update the display. */
  1320.   {
  1321.     int i, j;
  1322.     char *tempa, *temp;
  1323.  
  1324.     VSIwrapnow(&i, &j);
  1325.  
  1326.     tempa = VSIw->attrst[VSIw->y]->text;
  1327.     temp = VSIw->linest[VSIw->y]->text;
  1328.     for (i = VSIw->maxwidth - x; i >= VSIw->x; i--)
  1329.       {
  1330.       /* move along remaining characters on line */
  1331.         temp[x + i] =temp[i];
  1332.         tempa[x + i] = tempa[i];
  1333.       } /* for */
  1334.     for (i = VSIw->x; i < VSIw->x + x; i++)
  1335.       {
  1336.       /* insert appropriate number of blanks */
  1337.         temp[i] = ' ';
  1338.         tempa[i] = VSIclrattrib;
  1339.       } /* for */
  1340.   } /* VSIinschar */
  1341.  
  1342. void VSIinsstring
  1343.   (
  1344.     int len,
  1345.     char *start
  1346.   )
  1347.   /* updates the screen to show insertion of a string of characters
  1348.     at the current cursor position. The text has already been
  1349.     inserted into the screen buffer. Also, the cursor position has
  1350.     already been updated, so the part needing redrawing begins at column
  1351.     (VSIw->x - len). */
  1352.   {
  1353.     if (VSIw->VSIDC)
  1354.         RSinsstring(VSIwn, VSIw->x - len, VSIw->y,
  1355.             VSIw->attrib, len, start);
  1356.     else
  1357.       /* redraw from beginning of text insertion to end of line.
  1358.         Trouble is, this isn't going to preserve the right attributes. */
  1359.         RSdraw(VSIwn, VSIw->x - len, VSIw->y, VSIw->attrib,
  1360.             VSIw->maxwidth - VSIw->x + len + 1, start);
  1361.   } /* VSIinsstring */
  1362.  
  1363. void VSIsave
  1364.   (
  1365.     void
  1366.   )
  1367.   /* saves the current cursor position and attribute settings. */
  1368.   {
  1369.     VSIw->Px = VSIw->x;
  1370.     VSIw->Py = VSIw->y;
  1371.     VSIw->Pattrib = VSIw->attrib;
  1372.   } /* VSIsave */
  1373.  
  1374. void VSIrestore
  1375.   (
  1376.     void
  1377.   )
  1378.   /* restores the last-saved cursor position and attribute settings. */
  1379.   {
  1380.     if (VSIw->Pattrib < 0)
  1381.       /* no previous save */
  1382.         return;
  1383.         
  1384.     VSIw->x = VSIw->Px;
  1385.     VSIw->y = VSIw->Py;
  1386.     VSIrange();
  1387.     VSIw->attrib = VSinattr(VSIw->Pattrib); /* hmm, this will clear the graphics character set selection */
  1388.   } /* VSIrestore */
  1389.  
  1390. void VSIdraw
  1391.   (
  1392.     int VSIwn, /* window number */
  1393.     int x, /* starting column */
  1394.     int y, /* line on which to draw */
  1395.     int a, /* text attributes */
  1396.     int len, /* length of text to draw */
  1397.     char *c /* pointer to text */
  1398.   )
  1399.   /* displays a piece of text (assumed to fit on a single line) on a
  1400.     screen, using the specified attributes, and clipping to the
  1401.     current visible region. Highlights any part of the text lying
  1402.     within the current selection. */
  1403.   {
  1404.     int x2, y2, offset;
  1405.  
  1406.     if (!VSIclip(&x, &y, &x2, &y2, &len, &offset))
  1407.         RSdraw(VSIwn, x, y, a, len, (char *) (c + offset));    /* BYU LSC */
  1408.   } /* VSIdraw */
  1409.